package com.coin.exchanges.view.fragment.trade.position;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.coin.exchanges.R;
import com.coin.exchanges.adapter.NewMarketAdapter;
import com.coin.exchanges.adapter.PositionContentAdapter;
import com.coin.exchanges.config.FragmentConfig;
import com.coin.exchanges.model.ResultBean;
import com.coin.exchanges.model.haveH;
import com.coin.exchanges.model.okex.vo.RankItemVO;
import com.coin.exchanges.mvp.KLine.KLineActivity;
import com.coin.exchanges.net.RetrofitFactory;
import com.coin.exchanges.utils.AppUtils;
import com.coin.exchanges.utils.LCSharedPreferencesHelper;
import com.coin.libbase.utils.ToastUtil;
import com.coin.libbase.view.fragment.JBaseFragment;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * @author Jiang zinc
 * @date 创建时间：2018/11/21
 * @description 交易界面 —— 持仓fragment
 */

public class PositionFragment extends JBaseFragment implements WheelSelectFragment.OnSelectedListener {

    public static final String ALL_TYPE = "全部";

    @BindView(R.id.tv_type)
    TextView tvType;
    @BindView(R.id.ll_type)
    LinearLayout llType;
    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    private List<String> mTypeList;

    private PositionContentFragment mFragment;
    private WheelSelectFragment mTypeFragment;

    private String mPlatform;
    private PositionContentAdapter mAdapter;

    public static PositionFragment newInstance(String platform) {

        Bundle bundle = new Bundle();
        bundle.putString(AppUtils.PLATFORM, platform);

        PositionFragment positionFragment = new PositionFragment();
        positionFragment.initArgs(bundle);

        return positionFragment;
    }

    @Override
    protected void initArgs(Bundle arguments) {
        super.initArgs(arguments);
        mPlatform = arguments.getString(AppUtils.PLATFORM);
    }

    @Override
    protected View onCreateFragmentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_position, container, false);
    }

    @Override
    protected void initView(View view) {
        mTypeList = new ArrayList<>();
        mTypeList.add("全部");
        initAdapter();
        initData();


//        mTypeFragment = new WheelSelectFragment();
//        mTypeFragment.setData(mTypeList);
//        mTypeFragment.setOnSelectedListener(this);

//        mFragment = PositionContentFragment.newInstance(mPlatform);
//        addLayerFragment(frameLayout.getId(), mFragment);

//        llType.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                mTypeFragment.show(PositionFragment.this);
//            }
//        });
    }

    private void initData() {
        LCSharedPreferencesHelper sharedPreferencesHelper = LCSharedPreferencesHelper.instance(getActivity(), "register");
        String userId = sharedPreferencesHelper.getValue("UserId");
        Map<String, String> map = new HashMap<>();
        map.put("user_id", userId);
        RetrofitFactory
                .getOkExApiService()
                .haveCurrency(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<haveH>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(haveH resultBean) {
                        Gson gson = new Gson();
                        Log.i("dnlog", gson.toJson(resultBean));
                        if (resultBean.getResult() == 0) {
                            mAdapter.setNewData(resultBean.getData());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    List<haveH.DataBean> mLists = new ArrayList<>();

    private void initAdapter() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mAdapter = new PositionContentAdapter(R.layout.item_currency, mLists);
//        View headView = LayoutInflater.from(getActivity()).inflate(R.layout.item_rank_title, null);
//        mAdapter.setHeaderView(headView);
        mRecyclerView.setAdapter(mAdapter);
//        mSwipeRereshLayout.setRefreshing(true);
    }

    public void setTypeList(List<String> typeList) {
        mTypeList.clear();
        mTypeList.add(ALL_TYPE);
        mTypeList.addAll(typeList);
        tvType.setText(ALL_TYPE);
    }

    @Override
    public void getData(int index, String data) {
        tvType.setText(data);
//        mFragment.setType(data);
    }
}
